<?php
namespace ExMongodb;

use MongoDB\Client;
use think\Config;

class Mongod{
    
    static public $mongo = null;
    static public $options = [
        'host'       => '111',
        'port'       => 123,
        'password'   => 'xxx',
        'user'     => 'test',
        'database' =>'test'
    ];
        
    public static function getInstance(){
        
        if (!extension_loaded('mongodb')) {
            throw new Exception('_NOT_SUPPERT_:Mongodb');
        }
        
        if(is_null(self::$mongo)){
            self::$options = array_merge(self::$options, Config::get('mongodb'));
            $link =  'mongodb://'.self::$options['user'].":".rawurlencode(self::$options['password']).'@'.self::$options['host'].':'.self::$options['port'].'/'.self::$options['database'].'?connectTimeoutMS=3000';
            self::$mongo = new Client($link);
        }
        
        return self::$mongo;
    }
    
    public static function store($table,$data_obj){
        $collection = self::getInstance()->selectCollection(self::$options['database'],$table);
        return $collection->insertOne($data_obj);
    }
    
    public static function getCollect($table){
       return self::getInstance()->selectCollection(self::$options['database'],$table);
    }
    
}